/**
 * 
 */
package ru.sis.shop.filters;

import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;

/**
 * @author sis
 * 
 */
public final class LoginRequestWrapper extends HttpServletRequestWrapper {
	private static final String ROLE_DELIMITERS = ", \t\n\r\f";
	
	public LoginRequestWrapper(HttpServletRequest request) {
		super(request);
	}

	@SuppressWarnings("deprecation")
	public Map<String, Boolean> getIsUserInRole() {
		return new MapMaker().makeComputingMap(new Function<String, Boolean>() {

			@Override
			@Nullable
			public Boolean apply(@Nullable String roles) {
				StringTokenizer tokenizer = new StringTokenizer(roles, ROLE_DELIMITERS);
				boolean result = false;
				
				while (tokenizer.hasMoreTokens() && !result) {
					if (isUserInRole(tokenizer.nextToken())) {
						result = true;
					}
				}
				
				return result;
			}
		});
	}
}
